<?php
namespace app\index\controller;
use app\index\controller\BaseController;

class MemberController  extends BaseController {

	protected function _initialize() {
		parent::_initialize();
	}

	/**
	 * 会员中心
	 */
	public function index() {

		return $this->fetch();
	}

	/**
	 * 修改资料
	 */
	public function update() {
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$data['fans'] = implode(',', input('fans/a'));
			$area = explode('-', $data['area']);
			unset($data['area']);
			$data['qid'] = $area[0];
			$data['zid'] = $area[1];
			$data['cid'] = $area[2];
			//修改
			$result = db('member')->where('id', cookie('USER')['id'])->update($data);
			if ($result !== false) {
				$this->success('保存成功');
			} else {
				$this->error('保存失败');
			}
		} else {
			$model = db('member')->find(cookie('USER')['id']);
			$model['fans'] = explode(',', $model['fans']);
			if ($model['qid'] && $model['zid'] && $model['cid']) {
				$area[0] = $model['qid'];
				$area[1] = $model['zid'];
				$area[2] = $model['cid'];
				$model['area'] = implode('-', $area);
			} else {
				$model['area'] = '';
			}
			$this->assign('model', $model);
			// 行业父类
			$goods = db('product_cate')->where('pid', 0)->select();
			$this->assign('goods', $goods);
			return $this->fetch();
		}

	}

	/**
	 * 农家乐
	 */
	public function farmstay() {
		$model = db('farmstay')->where('uid', cookie('USER')['id'])->find();
		if ($this->request->isPost()) {
			$data = $this->request->post();
			if (input('thumbs/a')) {
				$data['thumbs'] = implode(',', input('thumbs/a'));
			} else {
				$data['thumbs'] = '';
			}
			$data['cate_id'] = implode(',', input('cate_id/a'));
			$area = explode('-', $data['area']);
			unset($data['area']);
			$data['qid'] = $area[0];
			$data['zid'] = $area[1];
			$data['cid'] = $area[2];
			if ($model) {
				// 修改
				$data['update_time'] = now_time();
				$result = db('farmstay')->where('uid', cookie('USER')['id'])->update($data);
			} else {
				// 添加
				$data['add_time'] = now_time();
				$data['uid'] = cookie('USER')['id'];
				$result = db('farmstay')->insert($data);
			}
			if ($result !== false) {
				$this->get_code(config('aliyun'), config('aliyun.shenghe_id'), "18670279299", "信息变更验证码");
				$this->success('保存成功');
			} else {
				$this->error('保存失败');
			}
		} else {
			if ($model) {
				if ($model['qid'] && $model['zid'] && $model['cid']) {
					$area[0] = $model['qid'];
					$area[1] = $model['zid'];
					$area[2] = $model['cid'];
					$model['area'] = implode('-', $area);
				} else {
					$model['area'] = '';
				}
				$model['cate_id'] = explode(',', $model['cate_id']);
				$model['thumbs'] = $model['thumbs'] == '' ? '' : explode(',', $model['thumbs']);
			} else {
				$model['area'] = '';
				$model['cate_id'] = array();
				$model['thumbs'] = '';
				$model['status'] = 1;
				$model['auditing'] = 0;
			}
			$this->assign('model', $model);
			// 行业父类
			$cate = db('farmstay_cate')->select();
			$this->assign('cate', $cate);
			return $this->fetch();
		}
	}

	/**
	 * 农机租赁
	 */
	public function machine() {
		$model = db('machine')->where('uid', cookie('USER')['id'])->find();
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$area = explode('-', $data['area']);
			unset($data['area']);
			$data['qid'] = $area[0];
			$data['zid'] = $area[1];
			$data['cid'] = $area[2];
			if ($model) {
				// 修改
				$data['update_time'] = now_time();
				$result = db('machine')->where('uid', cookie('USER')['id'])->update($data);
			} else {
				// 添加
				$data['uid'] = cookie('USER')['id'];
				$data['add_time'] = now_time();
				$result = db('machine')->insert($data);
			}
			if ($result !== false) {
				$this->get_code(config('aliyun'), config('aliyun.shenghe_id'), "18670279299", "信息变更验证码");
				$this->success('保存成功');
			} else {
				$this->error('保存失败');
			}
		} else {
			if ($model) {
				if ($model['qid'] && $model['zid'] && $model['cid']) {
					$area[0] = $model['qid'];
					$area[1] = $model['zid'];
					$area[2] = $model['cid'];
					$model['area'] = implode('-', $area);
				} else {
					$model['area'] = '';
				}
			} else {
				$model['area'] = '';
				$model['cate_id'] = 0;
				$model['status'] = 1;
				$model['is_chushou'] = 1;
				$model['auditing'] = 0;
			}
			$this->assign('model', $model);
			// 行业父类
			$cate = db('machine_cate')->select();
			$this->assign('cate', $cate);
			return $this->fetch();
		}
	}

	//申请发布功能
	public function shop() {
		$uid = cookie('USER')['id'];
		$model = db('shop')->where('uid', $uid)->find();
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$area = explode('-', $data['area']);
			unset($data['area']);
			$data['qid'] = $area[0];
			$data['zid'] = $area[1];
			$data['cid'] = $area[2];
			if ($model) {
				$data['update_time'] = now_time();
				$result = db('shop')->where('uid', $uid)->update($data);
			} else {
				$data['add_time'] = now_time();
				$data['uid'] = $uid;
				$result = db('shop')->insert($data);
			}
			if ($result !== false) {
				$this->success('保存成功');
			} else {
				$this->error('保存失败');
			}
		} else {
			if ($model['qid'] && $model['zid'] && $model['cid']) {
				$area[0] = $model['qid'];
				$area[1] = $model['zid'];
				$area[2] = $model['cid'];
				$model['area'] = implode('-', $area);
			} else {
				$model['area'] = '';
				$model['type'] = '';
			}
			$this->assign('model', $model);
			return $this->fetch();
		}
	}

	//发布生产数据
	public function product() {
		$list = db('product')->where('uid', cookie('USER')['id'])->order('add_time', 'desc')->select();
		$this->assign('list', $list);
		return $this->fetch();
	}

	public function product_cates($pid = 0) {
		if (!is_numeric($pid)) {
			$this->error('参数错误');
		}
		$cates = db('news_cate')->where('pid', $pid)->select();
		if ($cates) {
			$this->success('获取成功', '', $cates);
		} else {
			$this->error('此分类没有产品');
		}
	}

	public function product_items($pid = 0) {
		if (!is_numeric($pid)) {
			$this->error('参数错误');
		}
		$cates = db('news_cate')->where('pid', $pid)->select();
		if ($cates) {
			$this->success('获取成功' . $pid, '', $cates);
		} else {
			$this->error('此分类没有类目');
		}
	}

	/**
	 * 农产品发布
	 */
	public function product_add() {
		$shop = db('shop')->where('uid', cookie('USER')['id'])->find();
		if (!$shop) {
			$this->error('请填写生产者资料才可以发布产品', url('member/shop'));
		}
		if ($this->request->isPost()) {
			$data = $this->request->post();
			if (input('thumbs/a')) {
				$data['thumbs'] = implode(',', input('thumbs/a'));
			} else {
				$data['thumbs'] = '';
			}
			$area = explode('-', $data['area']);
			unset($data['area']);
			$data['qid'] = $area[0];
			$data['zid'] = $area[1];
			$data['cid'] = $area[2];
			$data['uid'] = cookie('USER')['id'];
			$shop_id = db('shop')->where('uid', cookie('USER')['id'])->value('id');
			$data['shop_id'] = $shop_id;
			$data['add_time'] = now_time();
			$result = db('product')->insert($data);
			if ($result !== false) {
				$this->get_code(config('aliyun'), config('aliyun.shenghe_id'), "18670279299", "信息变更验证码");
				$this->success('发布成功', url('member/product'));
			} else {
				$this->error('发布失败');
			}
		} else {
			$cate = db('news_cate')->where('status',1)->where('pid', 0)->select();
			$this->assign('cate', $cate);
			$cate2 = db('news_cate')->where('status',1)->where('pid', $cate[0]['id'])->select();
			$this->assign('cate2', $cate2);
			$cate3 = db('news_cate')->where('status',1)->where('pid', $cate2[0]['id'])->select();
			$this->assign('cate3', $cate3);
			return $this->fetch();
		}
	}

	/**
	 * 农产品修改
	 */
	public function product_update($id) {
		if (!is_numeric($id)) {
			$this->error('参数错误');
		}
		$model = db('product')->where('uid', cookie('USER')['id'])->where('id', $id)->find();
		if (!$model) {
			$this->error('产品大数据不存在！');
		}
		if ($this->request->isPost()) {
			$data = $this->request->post();
			if (input('thumbs/a')) {
				$data['thumbs'] = implode(',', input('thumbs/a'));
			} else {
				$data['thumbs'] = '';
			}
			$area = explode('-', $data['area']);
			unset($data['area']);
			$data['qid'] = $area[0];
			$data['zid'] = $area[1];
			$data['cid'] = $area[2];
			$data['update_time'] = now_time();
			$result = db('product')->update($data, $id);
			if ($result !== false) {
				$this->success('修改成功', url('member/product'));
			} else {
				$this->error('修改失败');
			}
		} else {
			if ($model['thumbs']) {
				$model['thumbs'] = explode(',', $model['thumbs']);
			}
			if ($model['qid'] && $model['zid'] && $model['cid']) {
				$area[0] = $model['qid'];
				$area[1] = $model['zid'];
				$area[2] = $model['cid'];
				$model['area'] = implode('-', $area);
			} else {
				$model['area'] = '';
			}
			$cate = db('news_cate')->where('pid', 0)->select();
			$this->assign('cate', $cate);
			$cate2 = db('news_cate')->where('pid', $model['cate_id1'])->select();
			$this->assign('cate2', $cate2);
			$cate3 = db('news_cate')->where('pid', $model['cate_id2'])->select();
			$this->assign('cate3', $cate3);
			$this->assign('model', $model);
			return $this->fetch();
		}
	}

	/**
	 * 农产品删除
	 */
	public function product_del($id) {
		if (!is_numeric($id)) {
			$this->error('参数错误');
		}
		$result = db('product')->where('id', $id)->where('uid', cookie('USER')['id'])->delete();
		if ($result) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
	}
	/**
	 * 生产日志列表
	 */
	public function pro_log_list($pid) {
		if (!is_numeric($pid)) {
			$this->error('参数错误');
		}
		$list = db('pro_log')->where('pid', $pid)->order('add_time')->select();
		$this->assign('list', $list);
		$this->assign('data', db('product')->find($pid));
		return $this->fetch();
	}
	
	
	
	public function invite() {
		$id = cookie('USER')['id'];
		$data = db('member')->find($id);
		$this->assign('data', $data);
		return $this->fetch();
	}

	//意见反馈
	function feedback() {
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$data['add_time'] = now_time();
			$data['u_id'] = cookie('USER')['id'];
			$res = db('complain')->insert($data);
			if ($res) {
				$this->success('提交成功', url('member/index'));
			} else {
				$this->error('提交失败');
			}
		} else {
			return $this->fetch();
		}
	}

	public function about() {
		return $this->fetch();
	}

	public function setting() {
		return $this->fetch();
	}

	public function help() {
		if (input('fid')) {
			$fid = input('fid');
		} else {
			$fid = 1;
		}
		$cate = db('sxy_cate')->where('id', $fid)->value('name');
		$this->assign('cate', $cate);
		$list = db('sxy')->where('cate_id1', $fid)->order('add_time desc')->select();
		$this->assign('list', $list);
		return $this->fetch();
	}

	public function help_show($id) {
		if (!is_numeric($id)) {
			$this->error('参数错误');
		}
		db('sxy')->where('id', $id)->setInc('clicks');
		$data = db('sxy a')->join('__SXY_CATE__ g', 'a.cate_id2 = g.id')->field('a.*,g.name')->where('a.id', $id)->find();
		$this->assign('data', $data);
		return $this->fetch();
	}

	public function dingyue() {
		$user = db('member')->find(cookie('USER')['id']);
		$ddd=ceil((strtotime($user['js_time']) - strtotime(date('Y-m-d')))/24/60/60);
		$this->assign('ddd', $ddd);
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$order['uid'] = cookie('USER')['id'];
			$order['sn'] = build_order_no();
			$order['total'] = $data['total'];
			$order['title'] = '农技电子杂志';
			$order['pay_type'] = $data['pay_type'];
			$order['yuetype1'] = implode(',', input('yuetype1/a'));
			$order['ks_time'] = now_time();
			$order['yuetype'] = implode(',', input('yuetype/a'));
			if(strtotime($user['js_time'])-strtotime(date('Y-m-d')) > 0){
				$dd = strtotime($user['js_time']);
			}else{
				$dd = strtotime(date('Y-m-d'));
			}
			
			
			if($data['num']==200){
				$order['js_time']=date('Y-m-d', strtotime ("+365 day", $dd));
			}elseif($data['num']==120){
				$order['js_time']=date('Y-m-d', strtotime ("+180 day", $dd));
			}elseif($data['num']==70){
				$order['js_time']=date('Y-m-d', strtotime ("+90 day", $dd));
			}else{
				$order['js_time']=date('Y-m-d', strtotime ("+30 day", $dd));
			}
			$result = db('dingyue_log')->insert($order);
			if ($result) {
				$this->redirect(url('dingyue/dopay', array('sn' => $order['sn'])));
			} else {
				$this->error('订阅失败');
			}
			
		}
		$cate = db('news_cate')->where('pid', 0)->where('status',1)->order('sort')->select();
		for ($i = 0; $i < count($cate); $i++) {
			$cate2 = db('news_cate')->where('pid', $cate[$i]['id'])->where('status',1)->select();
			$cate[$i]['note'] = $cate2;
		}
		$this->assign('cate', $cate);
		$user['yuetype1'] = explode(',', $user['yuetype1']);
		$user['yuetype'] = explode(',', $user['yuetype']);
		$this->assign('user', $user);
		
		return $this->fetch();
	}
	public function address() {
		$list = db('address')->where('uid', cookie('USER')['id'])->order('add_time desc')->select();
		$this->assign('list', $list);
		return $this->fetch();
	}
	public function address_add() {
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$area = explode('-', $data['area']);
			unset($data['area']);
			$data['qid'] = $area[0];
			$data['zid'] = $area[1];
			$data['cid'] = $area[2];
			$data['uid'] = cookie('USER')['id'];
			$data['add_time'] = now_time();
			db('address')->where('uid',$data['uid'])->update(['isdefault'=>'2']);
			$result = db('address')->insert($data);
			
			if ($result !== false) {
				$this->success('新增成功', url('member/address'));
			} else {
				$this->error('新增失败');
			}
		} else {
			return $this->fetch();
		}
	}
	public function address_update($id) {
		if (!is_numeric($id)) {
			$this->error('参数错误');
		}
		$model = db('address')->where('uid', cookie('USER')['id'])->where('id', $id)->find();
		if (!$model) {
			$this->error('地址不存在！');
		}
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$area = explode('-', $data['area']);
			unset($data['area']);
			$data['qid'] = $area[0];
			$data['zid'] = $area[1];
			$data['cid'] = $area[2];
			$data['uid'] = cookie('USER')['id'];
			db('address')->where('uid',$data['uid'])->update(['isdefault'=>'2']);
			$result = db('address')->where('id', $id)->update($data);
			if ($result !== false) {
				$this->success('修改成功', url('member/people'));
			} else {
				$this->error('修改失败');
			}
		} else {
			if ($model['qid'] && $model['zid'] && $model['cid']) {
				$area[0] = $model['qid'];
				$area[1] = $model['zid'];
				$area[2] = $model['cid'];
				$model['area'] = implode('-', $area);
			} else {
				$model['area'] = '';
			}
			$this->assign('model', $model);
			return $this->fetch();
		}
	}
	public function address_del($id) {
		if (!is_numeric($id)) {
			$this->error('参数错误');
		}
		$result = db('address')->where('id', $id)->where('uid', cookie('USER')['id'])->delete();
		if ($result) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
	}
	
	public function tixian() {
		return $this->fetch();
	}
	public function yue_record() {
		return $this->fetch();
	}
	public function score_record() {
		return $this->fetch();
	}
	
	
	
	
}
